﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;

namespace FetchDataCompare
{
	internal class WireGeoObj : GeoObjectBase
	{
		public WireGeoObj(Element inputElement)
		{
			base.GeoObjType = EGeoObjectType.eDimObj;
			this.InitDataByElement(inputElement);
		}

		public override void InitDataByElement(Element inputElement)
		{
			Wire wire = inputElement as Wire;
			if (wire == null)
			{
				base.BoundingRect.LeftBottom = XYZ.Zero;
				base.BoundingRect.RightBottom = XYZ.Zero;
				base.BoundingRect.LeftTop = XYZ.Zero;
				base.BoundingRect.RightTop = XYZ.Zero;
				return;
			}
			try
			{
				LocationCurve locationCurve = wire.Location as LocationCurve;
				this.InitBoundingRectByLine(locationCurve.Curve as Line, 0.05, XYZ.BasisZ);
			}
			catch (Exception)
			{
				base.BoundingRect.LeftBottom = XYZ.Zero;
				base.BoundingRect.RightBottom = XYZ.Zero;
				base.BoundingRect.LeftTop = XYZ.Zero;
				base.BoundingRect.RightTop = XYZ.Zero;
			}
		}

		private void InitBoundingRectByLine(Line line, double value, XYZ normal)
		{
			XYZ origin = line.Origin;
			XYZ direction = line.Direction;
			XYZ xyz = origin.Add(direction * line.Length);
			Transform transform = Transform.CreateRotationAtPoint(normal, -Math.PI*.5, origin);
			Transform transform2 = Transform.CreateRotationAtPoint(normal, Math.PI*.5, origin);
			XYZ leftBottom = origin.Add(transform2.OfVector(direction) * value);
			XYZ leftTop = origin.Add(transform.OfVector(direction) * value);
			XYZ rightBottom = xyz.Add(transform2.OfVector(direction) * value);
			XYZ rightTop = xyz.Add(transform.OfVector(direction) * value);
			base.BoundingRect.LeftBottom = leftBottom;
			base.BoundingRect.RightBottom = rightBottom;
			base.BoundingRect.LeftTop = leftTop;
			base.BoundingRect.RightTop = rightTop;
		}

		public override bool Equals(object obj)
		{
			return base.Equals(obj);
		}
	}
}
